<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>验证栈序列</title>
</head>
<body>
  <script>
    function fn(arr1, arr2) {
      const res = []
      let i = 0
      for (const num of arr1) {
        res.push(num)
        while (res.length > 0 && res[res.length - 1] === arr2[i]) {
          res.pop()
          i++
        }
      }
      return i === arr2.length
    }
    console.log(fn([1, 2, 3, 4, 5], [4, 5, 3, 2, 1]))
  </script>
</body>
</html>